package com.ericom.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
@Entity
public class City extends BaseObject {
	private Long id;
	private String code;    
    @Override
	public String toString() {
		return new ToStringBuilder(this).append("id", id).append("code", code)
				.append("name", name).append("province", province)
				.append("districts", districts).toString();
	}

	@Override
	public boolean equals(final Object other) {
		if (!(other instanceof City))
			return false;
		City castOther = (City) other;
		return new EqualsBuilder().append(code, castOther.code)
				.append(name, castOther.name)
				.append(province, castOther.province)
				.isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(code).append(name).append(province)
				.toHashCode();
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	private String name;
    private Province province;
    private Set<District> districts=new HashSet<District>();
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	@ManyToOne(cascade={CascadeType.MERGE})
	@JoinColumn(name="provinceId")
	public Province getProvince() {
		return province;
	}

	public void setProvince(Province province) {
		this.province = province;
	}
	@OneToMany(mappedBy="city",cascade=CascadeType.ALL,fetch=FetchType.LAZY,targetEntity=District.class)
	public Set<District> getDistricts() {
		return districts;
	}
	
	public void setDistricts(Set<District> districts) {
		this.districts = districts;
	}

}
